<?php

/**
 *

C:\wamp\bin\php\php5.4.12\php.exe -f C:\phproot\rush\trunk\Cli\jd_receive_auto.php -c C:\wamp\bin\apache\apache2.4.4\bin\php.ini

/usr/local/bin/php -f /usr/home/koubei/Cli/jd_to_poptop.php


 */
set_time_limit(0);

define('ROOT_PATH', str_replace('Cli/'.basename(__FILE__), '', str_replace('\\', '/', __FILE__)));

require_once(ROOT_PATH."Cls/const.php");
require_once(ROOT_PATH."Cls/common.php");
require_once(ROOT_PATH."Cls/db.php");
require_once(ROOT_PATH."Cls/Snoopy.class.php");
require_once(ROOT_PATH."Cls/lib_jd.php");
require_once(ROOT_PATH."Cls/yunip.class.php");
require_once(ROOT_PATH."Cls/curl.php");


//请求
$cCurl  = new Curl();
$db     = new DB();

//代理IP使用限制次数
$proxy_use_limit = 10;

//任务开始结束时间
$order_begin_time   = 0;
$order_end_time     = 0;

/*************收货确认*************/

do{

    //开始时间
    $order_begin_time   = time();


    /*
    if(at_rest_time_range()){
        //休息
        cli_out('at rest time sleep 10 minutes');
        sleep(600);
        continue;
    }
    */

    cli_out('');
    cli_out('');
    cli_out('');
    cli_out('************************');


    //总订单数
    $all_submit_orders = get_receive_submit_order_count();
    cli_out('all_submit_orders_count:'.$all_submit_orders);

    //获取一个订单
    $order = get_one_receive_submit_goods();

    if(!empty($order)){

        //有订单
        $task_id = $order['task_id'];
        $order_id = $order['order_id'];
        $order_sn = $order['order_sn'];
        $user_id  = $order['user_id'];
        $goods_id = $order['goods_id'];

        //订单号
        cli_out('task_id:'.$task_id.' user_id:'.$user_id.' order_id:'.$order_id.' order_sn:'.$order_sn);

        cli_out('goods_id:'.$goods_id);

        //最后一个已收货的同店铺的订单
        $last_order = get_last_received_order_goods(array('goods_id'=> $goods_id));




        if(!empty($last_order)){
            //判断时间差
            $seconds_span = seconds_span($last_order['received_time'],full_date());

            cli_out('last order_id:'.$last_order['order_id'].' user_id:'.$user_id.' received_time:'.$last_order['received_time'].' now time:'.full_date().' seconds_span:'.$seconds_span);

            $wait = 30;
            if($seconds_span < $wait){
                $wait_seconds = $wait - $seconds_span;
                cli_out('wait:'.$wait_seconds.' seconds');
                sleep($wait_seconds);
                cli_out('wait:'.$wait_seconds.' over continue');
            }else{
                cli_out('continue');
            }
            //超过1分钟就可以继续执行了
        }else{
            cli_out('no last received order');
        }







        //设置收货中
        $receiveing = order_receive_status_update($order_id,RECEIVE_RECEIVEING);

        cli_out('order_id:'.$order_id.' set receiveing ok');

        //设置订单id
        $cCurl->order_id = $order_id;

        //收货确认
        $receive_result = receive_old_jd_order($order);

        if($receive_result){

            //设置收货成功
            order_receive_status_update($order_id,RECEIVE_RECEIVED);

            cli_out('order_id:'.$order_id.' set received ok');

        }else{

            //设置收货失败
            order_receive_status_update($order_id,RECEIVE_FAIL);

            cli_out('order_id:'.$order_id.' set fail ok');
        }

        //结束时间
        $order_end_time     = time();

        //使用时间
        $use_time = $order_end_time - $order_begin_time;

        $begin_date = full_date($order_begin_time);
        $end_date   = full_date($order_end_time);

        cli_out("begin:$begin_date    end:$end_date    use_seconds:$use_time");

        cli_out('************************');

        //休息
        sleep_rand();

    }else{
        //全部已完成收货
        cli_out('all order finished');

        //休息
        cli_out('sleep 20 seconds');
        sleep(20);
    }

}while(true);